<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :form="true"
    :destroy-on-close="true"
    :width="800"
    v-model="visible"
    :title="isUpdate ? '修改' : '添加'"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      @submit.prevent=""
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="父区域" prop="parentId">
            <dept-select v-model="form.parentId" placeholder="请选择" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="区域等级" prop="reginLevel">
            <dict-data
              code="sys_normal_disable"
              v-model="form.status"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="区域类型" prop="reginType">
            <dict-data
              code="sys_normal_disable"
              v-model="form.status"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="区域名称" prop="reginName">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.leader"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="区域编码" prop="reginCode">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.leader"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="排序号" prop="orderNum">
            <el-input-number
              :min="0"
              :max="99999"
              v-model="form.orderNum"
              placeholder="请输入显示排序"
              controls-position="right"
              class="ele-fluid"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地图/示意图" prop="map">
            <el-upload
              class="avatar-uploader"
              action=""
              :show-file-list="false"
            >
              <img v-if="form.map" :src="form.map" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              clearable
              type="textarea"
              v-model="form.phone"
              placeholder="请输入"
              :maxlength="11"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { EleMessage, emailReg, phoneReg } from 'ele-admin-plus';
  import { useFormData } from '@/utils/use-form-data';
  import DeptSelect from './dept-select.vue';
  import { addDept, updateDept } from '@/api/system/dept';
  import { Plus } from '@element-plus/icons-vue';
  const props = defineProps({
    /** 修改回显的数据 */
    data: Object,
    /** 上级部门id */
    parentId: Number,
    /** 指定机构下拉数据 */
    organizationData: Array
  });

  const emit = defineEmits(['done']);

  /** 弹窗是否打开 */
  const visible = defineModel({ type: Boolean });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    deptId: void 0,
    parentId: void 0,
    deptName: '',
    orderNum: 0,
    leader: '',
    phone: '',
    email: '',
    status: '0'
  });

  /** 表单验证规则 */
  const rules = reactive({
    parentId: [
      {
        required: true,
        message: '请选择父区域',
        trigger: 'change'
      }
    ],
   reginLevel: [
      {
        required: true,
        message: '请选择区域等级',
        trigger: 'change'
      }
    ],
   reginType: [
      {
        required: true,
        message: '请选择区域类型',
        trigger: 'change'
      }
    ],
    reginName: [
      {
        required: true,
        message: '请输入区域名称',
        trigger: 'blur'
      }
    ],
    reginCode: [
      {
        required: true,
        message: '请输入区域编码',
        trigger: 'blur'
      }
    ],
    orderNum: [
      {
        required: true,
        message: '请输入排序号',
        trigger: 'blur'
      }
    ],
  });

  /** 关闭弹窗 */
  const handleCancel = () => {
    visible.value = false;
  };

  /** 保存编辑 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      const saveOrUpdate = isUpdate.value ? updateDept : addDept;
      saveOrUpdate({ ...form, parentId: form.parentId || 0 })
        .then((msg) => {
          loading.value = false;
          EleMessage.success({ message: msg, plain: true });
          handleCancel();
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error({ message: e.message, plain: true });
        });
    });
  };

  /** 监听弹窗打开 */
  watch(visible, () => {
    if (visible.value) {
      if (props.data) {
        assignFields({
          ...props.data,
          parentId: props.data.parentId || void 0
        });
        isUpdate.value = true;
      } else {
        resetFields();
        form.parentId = props.parentId;
        isUpdate.value = false;
      }
    }
  });
</script>

<style scoped>
  .avatar-uploader .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
   .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    text-align: center;
    border: 1px solid #f1f1f1;
  }
</style>
